"""
获取驱动的方法
定义一个基础类：定位元素，对元素进行基础操作
"""
import time

from selenium import webdriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait


def getDriver(browser="Chrome"):
    if browser=="Chrome":
        driver = webdriver.Chrome()
    elif browser=="Firefox": #Safari
        driver = webdriver.Firefox()
    else:
        driver = webdriver.Edge()

    return driver

class BasePage:
    def __init__(self,driver):
        self.driver= driver
    # 打开网页
    def open(self,url):
        self.driver.get(url)
        # 窗口最大化
        self.driver.maximize_window()
        # 隐式等待
        self.driver.implicitly_wait(5)


    # 退出浏览器
    def quit(self):
        self.driver.quit()

    # 关闭浏览器


    # 定位元素
    def locate_element(self,locator):
        """
        定位元素
        :param locator: 字符串，格式类似于id = xxx  或class name =    xxx
        :return: 定位的元素
        """
        # 切割字符串
        list1 = locator.split("=")
        # 处理字符串 设置locator
        locator=( list1[0].strip(), list1[1].strip())
        # print(locator)
        try:
            # 显式等待
            e = WebDriverWait(self.driver, 10, 0.5).until(expected_conditions.presence_of_element_located(locator=locator))
            return  e
        except Exception as e1:
            print(f"定位{locator}元素失败，失败原因{e1}")


    # 发送数据
    def send_keys(self,locator,value):
        self.locate_element(locator).send_keys(value)

    # 点击元素

    # 下拉框根据文本内容选择

    # 下拉框根据下标选择

    # 下拉框根据value选择

    #跳转到iframe

    # 切换到上一层

    # 切换到默认层







if __name__ == '__main__':
    d = getDriver()
    bp = BasePage(d)
    # 打开浏览器
    bp.open("http://www.baidu.com")
    # 定位元素
    loctor = "id=chat-textarea"
    bp.send_keys(loctor,"java")
    time.sleep(4)


